package com.masterclient.app.businessmodule.homemodule.view.adapter.promotions;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;

import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.commonbean.banner.BannerNewItemBean;
import com.masterclient.app.R;

import com.masterclient.app.businessmodule.homemodule.bean.banner.BannerNewBean;
import com.masterclient.app.modules.firstpager.fragments.interfaces.ItemClickListener;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class Ad0Adapter extends RecyclerView.Adapter {


    private Context context;

    private  List<BannerNewBean> list;
    private ItemClickListener itemClickListener;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public Ad0Adapter(Context context, List<BannerNewBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    //创建新View，被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad0_item,parent,false);
        RecyclerView.ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,  int position) {

        ViewHolder viewHolder = (ViewHolder) holder;

        int length = list.get(position).getList().size();
        for(int i = 0 ; i <length; i ++){
            View view = View.inflate(context,R.layout.ad0_item_item,null);
            view.setTag(i);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
//            View space = view.findViewById(R.id.space);
            BannerNewItemBean adGroup = list.get(position).getList().get(i);
            int width = ScreenUtils.getScreenWidth()/length;
            int height = width * Integer.parseInt(list.get(position).getHeight()) /Integer.parseInt(list.get(position).getWidth());
            imageView.getLayoutParams().height = height;
            imageView.getLayoutParams().width = width;
            GlideUtil.displayImage(context,adGroup.getImage(),imageView,R.mipmap.defaults,width,height);
            //App.picasso.load(list.get(position).get(i).getSh_image()).resize(ScreenUtils.dp2px(48),ScreenUtils.dp2px(48)).placeholder(R.mipmap.defaults).into(imageView);
//            if(i == 0){
                //params.leftMargin = 0;
//                space.setVisibility(View.INVISIBLE);
//            }else{
//                if(length == 3){
//                    params.leftMargin = 1;
//                }else{
//                    params.leftMargin = 2;
//                }
//                space.setVisibility(View.VISIBLE);
//            }
            viewHolder.gridLayout.addView(view);

            view.setOnClickListener(v->{
                itemClickListener.onItemClick(position,(int)view.getTag());
            //startActivitryWithBundle(CategoryDetailActivity.class,new String[][]{{"from","firstpage"},{"cid",categoryBean.getSh_id()},{"title",categoryBean.getSh_name()},{"image",categoryBean.getSh_image()}});
        });
        }

//        if(position == list.size() - 1){
//            viewHolder.space.setVisibility(View.GONE);
//        }else{
//            viewHolder.space.setVisibility(View.VISIBLE);
//        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item)
        public View item;
//        @BindView(R.id.space)
//        public View space;
        @BindView(R.id.gridLayout)
        public GridLayout gridLayout;

        public ViewHolder(View view){
            super(view);
            ButterKnife.bind(this,view);
        }
    }


}

